package com.whut.transportation;

import java.util.Calendar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.whut.transportation.entity.TrainInfo;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.utils.MyLog;
import com.whut.transportation.wheel.StrericWheelAdapter;
import com.whut.transportation.wheel.WheelView;


/**
 * 查找条件首界面
 * @author yuliyan
 *
 */
public class SearchActivity extends BaseActivity {
	private Spinner spinner01;//搜索信息下拉条
	private Spinner quming;//搜索信息下拉条
	private Button searchSubmit;
	private EditText search_con;
	
	
	private String qumingString;
	private ArrayAdapter arrayAdapter1;//第一个下拉条的适配器
	
	private String searchInfomation;//第一个下拉条选中的信息
	private String searchTime;//第二个下拉条选中的信息
	private String trainId;//第三个下拉条选中的信息
	private TrainInfo train;//存车辆信息
	
	private SharedPreferences preferences;//取出偏好信息
	
	private int minYear = 1970; // 最小年份
	private int fontSize = 13; // 字体大小
	private WheelView yearWheel, monthWheel, dayWheel, hourWheel, minuteWheel;
	public static String[] yearContent = null;
	public static String[] monthContent = null;
	public static String[] dayContent = null;
/*	public static String[] hourContent = null;
	public static String[] minuteContent = null;*/
	
	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_search);
		setTitleBarView(false, "搜索", -1, false);
		preferences=getSharedPreferences("userId", Context.MODE_PRIVATE);
		searchInfomation = getResources().getStringArray(R.array.infomation)[0];
		initContent();
	}
	@Override
	protected void findViewById() {
		//spinnerText3 = (TextView) findViewById(R.id.spinnerText3);
		spinner01 = (Spinner) findViewById(R.id.Spinner01);
		//spinner03 = (Spinner) findViewById(R.id.Spinner03);
		//spinnerTime = (Spinner) findViewById(R.id.spinnerTime);
		searchSubmit = (Button) findViewById(R.id.search_submit);
		search_con = (EditText) findViewById(R.id.search_con);
		quming = (Spinner) findViewById(R.id.quming);
	}
	@Override
	protected void afterFindViewId() {
		//从arrays.xml中取出information放入第一个下拉条中
		arrayAdapter1 = ArrayAdapter.createFromResource(mContext, R.array.infomation, android.R.layout.simple_spinner_item);
		arrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner01.setAdapter(arrayAdapter1);
		spinner01.setVisibility(View.VISIBLE);
	}

	@Override
	protected void processLogic() {
		
	}

	@Override
	protected void setListener() {
		searchSubmit.setOnClickListener(this);
		spinner01.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				//第一个下拉条改变时，改变searchInfomation的值
				searchInfomation = arg0.getItemAtPosition(arg2).toString();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		search_con.setOnClickListener(this);
		
		
		quming.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				String q = parent.getItemAtPosition(position).toString();
				if(q.equals("港站散货")){
					qumingString = "SH";
				} else if(q.equals("港站矿码头")){
					qumingString = "KMT";
				} else if(q.equals("港站一区")){
					qumingString = "YQ";
				} else if(q.equals("东站")){
					qumingString = "DZ1";
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_submit:
			searchSubmit();
			break;
		case R.id.search_con:
			show5();
			break;
		default:
			break;
		}
		super.onClick(v);
	}
	private void searchSubmit() {
		//取出输入框输入的信息
		String searchContext = search_con.getText().toString();
		//查询输入信息和列车信息是由searchInfomation决定的
		if(searchContext!=null&&searchContext.length()>0){
			Intent intent = new Intent(this,SearchTransportationActivity.class);
			intent.putExtra("searchCon", searchContext);
			if("装车信息查询".equals(searchInfomation)){
				intent.putExtra("searchType", "0");
				intent.putExtra("quming", qumingString);
				startActivity(intent);
			} else if("卸车信息查询".equals(searchInfomation)){
				intent.putExtra("searchType", "1");
				intent.putExtra("quming", qumingString);
				startActivity(intent);
			}
		} else {
			Toast.makeText(mContext, "请输入时间", Toast.LENGTH_SHORT).show();
		}
	}
	@Override
	protected void getDataAgain() {
	}
	
	
	private void show5() {
		View view = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.time_picker, null);
		Calendar calendar = Calendar.getInstance();
		int curYear = calendar.get(Calendar.YEAR);
		int curMonth = calendar.get(Calendar.MONTH) + 1;
		int curDay = calendar.get(Calendar.DAY_OF_MONTH);
		int curHour = calendar.get(Calendar.HOUR_OF_DAY);
		/*int curMinute = calendar.get(Calendar.MINUTE);
		int curSecond = calendar.get(Calendar.SECOND);*/

		yearWheel = (WheelView) view.findViewById(R.id.yearwheel);
		monthWheel = (WheelView) view.findViewById(R.id.monthwheel);
		dayWheel = (WheelView) view.findViewById(R.id.daywheel);
		/*hourWheel = (WheelView) view.findViewById(R.id.hourwheel);
		minuteWheel = (WheelView) view.findViewById(R.id.minutewheel);*/

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(view);

		yearWheel.setAdapter(new StrericWheelAdapter(yearContent));
		yearWheel.setCurrentItem(curYear - 2013);
		yearWheel.setCyclic(true);
		yearWheel.setInterpolator(new AnticipateOvershootInterpolator());

		monthWheel.setAdapter(new StrericWheelAdapter(monthContent));

		monthWheel.setCurrentItem(curMonth - 1);

		monthWheel.setCyclic(true);
		monthWheel.setInterpolator(new AnticipateOvershootInterpolator());

		dayWheel.setAdapter(new StrericWheelAdapter(dayContent));
		dayWheel.setCurrentItem(curDay - 1);
		dayWheel.setCyclic(true);
		dayWheel.setInterpolator(new AnticipateOvershootInterpolator());

		/*hourWheel.setAdapter(new StrericWheelAdapter(hourContent));
		hourWheel.setCurrentItem(curHour);
		hourWheel.setCyclic(true);
		hourWheel.setInterpolator(new AnticipateOvershootInterpolator());

		minuteWheel.setAdapter(new StrericWheelAdapter(minuteContent));
		minuteWheel.setCurrentItem(curMinute);
		minuteWheel.setCyclic(true);
		minuteWheel.setInterpolator(new AnticipateOvershootInterpolator());*/

		builder.setTitle("选取时间");
		builder.setPositiveButton("确  定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						StringBuffer sb = new StringBuffer();
						sb.append(yearWheel.getCurrentItemValue())
								.append(monthWheel.getCurrentItemValue())
								.append(dayWheel.getCurrentItemValue());
						/*sb.append(" ");
						sb.append(hourWheel.getCurrentItemValue()).append(":")
								.append(minuteWheel.getCurrentItemValue());*/
						MyLog.i("time", sb.toString() + "--------");
						search_con.setText(sb.toString());
						dialog.cancel();
					}
				});

		builder.show();
	}
	
	public void initContent() {
		yearContent = new String[10];
		for (int i = 0; i < 10; i++)
			yearContent[i] = String.valueOf(i + 2013);

		monthContent = new String[12];
		for (int i = 0; i < 12; i++) {
			monthContent[i] = String.valueOf(i + 1);
			if (monthContent[i].length() < 2) {
				monthContent[i] = "0" + monthContent[i];
			}
		}

		dayContent = new String[31];
		for (int i = 0; i < 31; i++) {
			dayContent[i] = String.valueOf(i + 1);
			if (dayContent[i].length() < 2) {
				dayContent[i] = "0" + dayContent[i];
			}
		}
		/*hourContent = new String[24];
		for (int i = 0; i < 24; i++) {
			hourContent[i] = String.valueOf(i);
			if (hourContent[i].length() < 2) {
				hourContent[i] = "0" + hourContent[i];
			}
		}

		minuteContent = new String[60];
		for (int i = 0; i < 60; i++) {
			minuteContent[i] = String.valueOf(i);
			if (minuteContent[i].length() < 2) {
				minuteContent[i] = "0" + minuteContent[i];
			}
		}*/
	}
}
